#include "queueLink.h"

QUEUE *createQueueLink()
{
    QUEUE *qlink = (QUEUE *)malloc(sizeof(QUEUE));
    ERROR_CHECK(NULL, qlink, "malloc");
    qlink->front = NULL;
    qlink->rear = NULL;
    return qlink;
}
void enQueue(QUEUE *qlink, type val)
{
    if (NULL == qlink)
    {
        puts("NULL param");
        exit(1);
    }
    QL *new_node = (QL *)malloc(sizeof(QL));
    ERROR_CHECK(NULL, new_node, "malloc new_node");
    new_node->data = val;
    new_node->next = NULL;
    if (qlink->rear == NULL)
    {
        qlink->front = new_node;
    }
    else
    {
        qlink->rear->next = new_node;
    }
    qlink->rear = new_node;
}
type deQueue(QUEUE *qlink)
{
    if (NULL == qlink)
    {
        puts("NULL param");
        exit(1);
    }
    if (isEmpty(qlink))
    {
        puts("empty");
        exit(1);
    }
    QL *temp = qlink->front;
    qlink->front = temp->next;
    int ret = temp->data;
    free(temp);
    if (qlink->front == NULL)
    {
        qlink->rear = NULL;
    }
    temp = NULL;
    return ret;
}
int isEmpty(QUEUE *qlink)
{
    if (NULL == qlink)
    {
        puts("NULL param");
        exit(1);
    }
    return qlink->rear == NULL ? 1 : 0;
}
void freeQueue(QUEUE **qlink)
{
    if (NULL == qlink)
    {
        puts("NULL param");
        exit(1);
    }
    init(*qlink);
    free(*qlink);
    *qlink = NULL;
}
void init(QUEUE *qlink)
{

    if (NULL == qlink)
    {
        puts("NULL param");
        exit(1);
    }
    while (qlink->front)
    {
        QL *temp = qlink->front;
        qlink->front = qlink->front->next;
        free(temp);
    }
    qlink->rear = NULL;
}